package com.ysqcould.web.controller.approval;


import com.ysqcould.approval.domain.Official;
import com.ysqcould.approval.service.IOfficialService;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公文Controller
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/approval/official")
public class OfficialController extends BaseController
{
    @Autowired
    private IOfficialService officialService;

    /**
     * 查询公文列表
     */
    @PreAuthorize("@ss.hasPermi('approval:official:list')")
    @GetMapping("/list")
    public TableDataInfo list(Official official)
    {
        startPage();
        List<Official> list = officialService.selectOfficialList(official);
        return getDataTable(list);
    }

    /**
     * 导出公文列表
     */
    @PreAuthorize("@ss.hasPermi('approval:official:export')")
    @Log(title = "公文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Official official)
    {
        List<Official> list = officialService.selectOfficialList(official);
        ExcelUtil<Official> util = new ExcelUtil<Official>(Official.class);
        util.exportExcel(response, list, "公文数据");
    }

    /**
     * 获取公文详细信息
     */
    @PreAuthorize("@ss.hasPermi('approval:official:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(officialService.selectOfficialById(id));
    }

    /**
     * 新增公文
     */
    @PreAuthorize("@ss.hasPermi('approval:official:add')")
    @Log(title = "公文", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Official official)
    {
        official.setAsId(getUserId());
        return toAjax(officialService.insertOfficial(official));
    }

    /**
     * 修改公文
     */
    @PreAuthorize("@ss.hasPermi('approval:official:edit')")
    @Log(title = "公文", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Official official)
    {
        return toAjax(officialService.updateOfficial(official));
    }

    /**
     * 删除公文
     */
    @PreAuthorize("@ss.hasPermi('approval:official:remove')")
    @Log(title = "公文", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(officialService.deleteOfficialByIds(ids));
    }
}
